<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use yii\web\Response;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\EntryForm;

class SiteController extends Controller
{
    /**
     * {@inheritdoc}
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    /**
     * Displays homepage.
     *
     * @return string
     */
    public function actionIndex()
    {

        return $this->render('index');
    }

    /**
     * Login action.
     *
     * @return Response|string
     */
    public function actionLogin()
    {
        if (!Yii::$app->user->isGuest) {
            return $this->goHome();
        }

        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        }

        $model->password = '';
        return $this->render('login', [
            'model' => $model,
        ]);
    }

    /**
     * Logout action.
     *
     * @return Response
     */
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }

    /**
     * Displays contact page.
     *
     * @return Response|string
     */
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        }
        return $this->render('contact', [
            'model' => $model,
        ]);
    }

    /**
     * Displays about page.
     *
     * @return string
     */
    public function actionAbout()
    {
        return $this->render('about');
    }


    public function actionSay($message = 'Hello')
    {
        return $this->render('say', ['message' => $message]);
    }

    public function actionEntry()
    {
        $model = new EntryForm;

        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            // 验证 $model 收到的数据

            // 做些有意义的事 ...

            return $this->render('entry-confirm', ['model' => $model]);
        } else {
            // 无论是初始化显示还是数据验证错误
            return $this->render('entry', ['model' => $model]);
        }
    }
    
    public function actionGeo(){
        $redis = new \Redis();
        $redis->connect('127.0.0.1', 6379);

        $redis->rawCommand('geoadd', 'citys', '116.40', '39.90', 'beijing');
        $redis->rawCommand('geoadd', 'citys', '121.47', '31.23', 'shanghai');
        $redis->rawCommand('geoadd', 'citys', '114.30', '30.60', 'wuhan');

        $redis->rawCommand('geoadd', 'citys', '116.40', '39.90', 'beijing');
        $redis->rawCommand('geoadd', 'citys', '121.47', '31.23', 'shanghai');
        $redis->rawCommand('geoadd', 'citys', '114.30', '30.60', 'wuhan');

        echo '<pre>';

//获取两个地理位置的距离，单位：m(米，默认)， km(千米)， mi(英里)， ft(英尺)
        var_dump($redis->rawCommand('geodist', 'citys', 'beijing', 'wuhan'));
        var_dump($redis->rawCommand('geodist', 'citys', 'beijing', 'shanghai', 'km'));

//获取成员的经纬度
        var_dump($redis->rawCommand('geopos', 'citys', 'shanghai'));

//获取成员的经纬度hash，geohash表示坐标的一种方法，便于检索和存储
        var_dump($redis->rawCommand('geohash', 'citys', 'shanghai', 'wuhan'));

//基于经纬度坐标的范围查询
//查询以经纬度为114，30为圆心，100千米范围内的成员
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km'));

//WITHCOORD表示获取成员经纬度
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHCOORD'));

//WITHDIST表示获取到圆心的距离
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHDIST'));

//WITHHASH表示获取成员经纬度HASH值
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'WITHHASH'));

//COUNT 数量，表示限制获取成员的数量
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'COUNT', '3'));

// ASC 根据圆心位置，从近到远的返回元素
// DESC 根据圆心位置，从远到近的返回元素
        var_dump($redis->rawCommand('georadius', 'citys', '114', '30', '100', 'km', 'ASC'));

//基于成员位置范围查询
//查询以武汉为圆心，100千米范围内的成员
        var_dump($redis->rawCommand('georadiusbymember', 'citys', 'wuhan', '100', 'km'));
    }
}
